package com.ihomey.linkuphome.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ihomey.linkuphome.R;
import com.ihomey.linkuphome.entities.Device;
import com.ihomey.linkuphome.entities.GroupDevice;
import com.ihomey.linkuphome.util.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/13.
 */

public class GroupListAdapter extends BaseAdapter {

    public interface OnGroupSettingListener {
        void onGroupSettingClick(int position);
    }

    public List<Device> devices;
    private boolean isShare;
    private LayoutInflater mInflater = null;
    private OnGroupSettingListener onGroupSettingListener;

    public void setOnGroupSettingListener(OnGroupSettingListener onGroupSettingListener) {
        this.onGroupSettingListener = onGroupSettingListener;
    }

    public GroupListAdapter(Activity activity, List<Device> devices, boolean isShare) {
        super();
        this.mInflater = LayoutInflater.from(activity);
        this.devices = devices == null ? new ArrayList<Device>() : devices;
        this.isShare = isShare;
    }

    @Override
    public int getCount() {
        return isShare ? devices.size() : devices.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return devices.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (!isShare) {
            if (position != getCount() - 1) {
                return 0;
            } else {
                return 1;
            }
        } else {
            return 0;
        }
    }

    private View createViewByType(int position) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case 0:
                return mInflater.inflate(R.layout.item_group, null);
            case 1:
                return mInflater.inflate(R.layout.item_group_footer, null);
        }
        return null;
    }

    public void addElem(Device groupDevice) {
        devices.add(groupDevice);
        notifyDataSetChanged();
    }

    public void remove(int index) {
        devices.remove(index);
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        if (convertView == null) convertView = createViewByType(position);
        ImageView iv_groupIcon = ViewHolder.get(convertView, R.id.iv_group_icon);
        TextView tv_groupName = ViewHolder.get(convertView, R.id.tv_group_name);
        TextView tv_groupSetting = ViewHolder.get(convertView, R.id.tv_group_setting);

        if (getItemViewType(position) == 0) {
            GroupDevice groupDevice = (GroupDevice) devices.get(position);
            if (groupDevice != null) {
                if (groupDevice.getDeviceType() == 1) {
                    iv_groupIcon.setImageResource(R.drawable.lamp_lawn);
                } else if (groupDevice.getDeviceType() == 2) {
                    iv_groupIcon.setImageResource(R.drawable.lamp_rgb);
                }
                tv_groupName.setText(groupDevice.getName());
            }

            if (isShare) {
                tv_groupSetting.setVisibility(View.INVISIBLE);
                tv_groupSetting.setOnClickListener(null);
            } else {
                tv_groupSetting.setVisibility(View.VISIBLE);
                tv_groupSetting.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (onGroupSettingListener != null) {
                            onGroupSettingListener.onGroupSettingClick(position);
                        }
                    }
                });
            }
        }
        return convertView;
    }


}
